import time
import uuid

from selenium.webdriver.support.select import Select

from DYing.day10.loctor02 import *
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Crmeb:

    uuid_ = str(uuid.uuid4()).split('-')[0]
    def __init__(self,driver,url):
        self.driver = driver
        self.driver.get(url)
        self.driver.implicitly_wait(15)
        self.driver.maximize_window()
    def login(self,user:str,passward:str):
        # 输入账户：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_ele))[0].send_keys(user)
        # 输入密码
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(password_ele))[1].send_keys(passward)
        # 点击登录
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(login_ele))[0].click()
    def add_user_group(self,name):
        self.name = name
        time.sleep(3)
        # 点击用户
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(add_cli_ele))[1].click()
        # 点击用户标签
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_cgr_ele)).click()
        # 点击添加
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_cad_ele)).click()
        # 点击标签分类
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_sel_ele)).click()
        # 选择
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_tap))[5].click()
        # 输入标签名称
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_ingroup_name_ele)).send_keys(self.name+str(self.uuid_))
        # 点击确定
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(add_cli_accept_ele))[2].click()
    def check_name(self):
        text = self.driver.find_element(By.XPATH,f"//*[text()='{self.name}{str(self.uuid_)}']").text
        assert text ==self.name+str(self.uuid_)
        time.sleep(3)
    def del_newadd(self):
        # 点击删除
        tr = driver.find_elements(By.CLASS_NAME, 'el-table__row')[-1]
        td = tr.find_elements(By.TAG_NAME, 'td')[-1]
        div = td.find_element(By.CLASS_NAME, 'cell')
        div.find_elements(By.TAG_NAME, 'a')[-1].click()
        time.sleep(3)
        # 确定删除
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(del_new_add)).click()

if __name__ == '__main__':
    driver = webdriver.Chrome()
    url = 'http://121.40.86.4:8899/admin'
    d_crmeb = Crmeb(driver,url)
    d_crmeb.login('admin','Aa010903!')
    d_crmeb.add_user_group("标签")
    d_crmeb.check_name()
    d_crmeb.del_newadd()
    d_crmeb.check_name()